from lxml import etree

from crawler_core.constant.constants import Constants
from crawler_core.constant.constants_api import ConstantAPI
from crawler_core.exception.analysis_result_exec import AnalysisResultException

from analysis.download.interface import DAInterface
from lib.sys.logger_factory import LoggerFactory


class DA51job(DAInterface):

    def __init__(self):
        pass

    def analysis_cache(self, html):
        try:
            message = etree.HTML(html)
        except BaseException as e:
            print(e.args)
            print(e)
            raise AnalysisResultException()
        # 4、解析结果，并返回
        try:
            name = "".join(message.xpath('//td[@id="tdseekname"]/text()')).strip()
        except AnalysisResultException:
            name = ""

        try:
            telephone = "".join(message.xpath('//table[@class="infr"]/tr[2]/td/table/tr/td[2]/text()')).strip()
        except AnalysisResultException:
            telephone = ""

        try:
            email = "".join(message.xpath('//table[@class="infr"]/tr[2]/td/table/tr/td[3]/table/tbody/tr/td[2]/a/text()')).strip()
        except AnalysisResultException:
            email = ""

        if telephone or email:
            code = ConstantAPI.RETURN_SUCCESS
        else:
            code = ConstantAPI.RETURN_ERROR_ANALYSIS
        data = {
            "name": name, "mobile": telephone, "email": email, Constants.DOWNLOAD_CODE: code
        }
        return data

